package net.diaowen.dwsurvey.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.diaowen.common.base.dao.GroupDao;
import net.diaowen.common.base.dao.UserDao;
import net.diaowen.common.base.entity.Group;
import net.diaowen.common.base.entity.User;
import net.diaowen.common.base.entity.UserGroup;
import net.diaowen.common.base.service.AccountManager;
import net.diaowen.common.plugs.page.Page;
import net.diaowen.common.service.BaseServiceImpl;
import net.diaowen.dwsurvey.entity.SurveyGroup;
import net.diaowen.dwsurvey.service.GroupManager;
import net.diaowen.dwsurvey.service.SurveyGroupManager;
import net.diaowen.dwsurvey.service.UserGroupManager;


/**
 * 分组管理
 * @author keyuan(keyuan258@gmail.com)
 *
 * https://github.com/wkeyuan/DWSurvey
 * http://dwsurvey.net
 */
@Service
public class GroupManagerImpl extends BaseServiceImpl<Group, String> implements GroupManager {
	

	@Autowired
	private GroupDao groupDao;
	
	@Autowired
	private UserDao userDao;

	@Override
	public void setBaseDao() {
		this.baseDao=groupDao;
	}
	
	@Autowired
	private UserGroupManager userGroupManager;
	
	@Autowired
	private AccountManager accountManager;
	
	@Autowired
	private SurveyGroupManager surveyGroupManager;
	

	@Override
	public List<Group> findList(Group entity) {
		List<Criterion> criterions=new ArrayList<Criterion>();
		String userGroupName = entity.getUserGroupName();
		if(userGroupName!=null && !"".equals(userGroupName)){
			criterions.add(Restrictions.like("userGroupName", "%"+userGroupName+"%"));
		}
		//超级管理员不进行机构过滤，其余进行过滤
		//如果后续条件再多的话，需要优化底层
		User curUser = accountManager.getCurUser();
		if(curUser.getSystemRole() == 8) {
			if (criterions.size() > 0) {
				return super.findList(criterions.get(0));
			} else {
				return super.findList();
			}
		} else {
			criterions.add(Restrictions.eq("orgId", curUser.getOrgId()));
			if (criterions.size() > 1) {
				return super.findList(criterions.get(0),criterions.get(1));
			} else {
				return super.findList(criterions.get(0));
			}
		}
	}

	@Override
	public void adminSave(Group entity) {
		if(entity!=null){
			save(entity);
		}
		
	}


	@Override
	public int deleteGroup(String id) {
		//先看看有没有发布的题目，有的话不可以删除该组
		List<SurveyGroup> list = surveyGroupManager.findListByGroupId(id);
		if (list.size() > 0) {
			//发布试卷的小组不可以删除
			return 1;
		} else {
			List<UserGroup> list2 = userGroupManager.findListByGroupId(id);
			if (list2.size() > 0) {
				for (UserGroup ug : list2) {
					userGroupManager.delete(ug.getId());
				}
				
			}
			delete(id);
			return 0;
		}
	}

	@Override
	public Group findByGroupNameAndOrgId(String groupName,String orgId) {
		List<Criterion> criterions=new ArrayList<Criterion>();
		criterions.add(Restrictions.eq("orgId", orgId));
		criterions.add(Restrictions.eq("userGroupName", groupName));
		return groupDao.findFirst(criterions);
	}

	@Override
	public Page<Group> findPage(Page<Group> page, Group entity) {
		List<Criterion> criterions=new ArrayList<Criterion>();
		String userGroupName = entity.getUserGroupName();
		if(userGroupName!=null && !"".equals(userGroupName)){
			criterions.add(Restrictions.like("userGroupName", "%"+userGroupName+"%"));
		}
		//超级管理员不进行机构过滤，其余进行过滤
		User curUser = accountManager.getCurUser();
		if(curUser.getSystemRole() == 8) {
			//
		} else {
			criterions.add(Restrictions.eq("orgId", curUser.getOrgId()));
		}
		return super.findPageByCri(page, criterions);
	}

	@Override
	public Group findOne(String id) {
		return groupDao.getModel(id);
	}

}
